package com.pinc.exam;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class QCount_Picker extends ListActivity {
	private ListView lv;
	private int mUpBound;
	private myArrayAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mUpBound = getIntent().getIntExtra("bound", 0);
		if ( 1 > mUpBound)
			finish();

		mAdapter = new myArrayAdapter(this, android.R.layout.simple_list_item_1);

		if ( mUpBound > 100)
		{
			mUpBound = 100;
			mAdapter.add(100 + " " + getResources().getString(R.string.str_question), 100);
		}
		else
			mAdapter.add(mUpBound + " " + getResources().getString(R.string.str_question) + " (" + getResources().getString(R.string.str_all) + ")", mUpBound);

		int top = (mUpBound-1)/5*5;

		for(int i=top;i>=5;i-=5)
		{
			mAdapter.add(i + " " + getResources().getString(R.string.str_question), i);
		}

		lv = getListView();
		lv.setAdapter(mAdapter);
		lv.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent();
				intent.putExtra("count", (int)id);
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
	}

	private class myArrayAdapter extends ArrayAdapter<String>
	{
		private ArrayList<Long> items;

		public myArrayAdapter(Context context, int textViewResourceId) {
			super(context, textViewResourceId);
			items = new ArrayList<Long>();
		}

		public void add(String object, long id) {
			super.add(object);
			items.add(id);
		}

		@Override
		public long getItemId(int position) {
			return items.get(position).longValue();
		}
	}

}
